﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel.Screens;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel;
using B10_Ex02_EladLebovitch_034695114.ObjectModel;
using Microsoft.Xna.Framework.Input;
using Infrastructure.ServiceInterfaces;

namespace B10_Ex02_EladLebovitch_034695114.Screens
{
    public class ScreenOptionsScreen : GameScreen
    {
        private Menu m_Menu;
        private InvadorBackground m_Background;
        private GraphicsDeviceManager m_GrapgMgr;

        private enum ScreenMenu
        {
            FullScreen,
            Resize,
            Mouse,
            Done
        }

        private string toStr(bool i_Val)
        {
            return (i_Val ? "On" : "Off");
        }

        public ScreenOptionsScreen(Game i_Game)
            : base(i_Game)
        {
            m_Background = new InvadorBackground(i_Game, @"Sprites\BG_Space01_1024x768");
            this.Add(m_Background);
            m_Menu = new Menu("Sprites\\ScreenMenu", i_Game, 4, 85);
            this.Add(m_Menu);
            m_Menu.MenuItemEntered += new EventHandler<MenuEventArgs>(m_Menu_MenuItemEntered);
            m_Menu.MenuItemPgDown += new EventHandler<MenuEventArgs>(m_Menu_MenuItemPgDown);
            m_Menu.MenuItemPgUp += new EventHandler<MenuEventArgs>(m_Menu_MenuItemPgDown);
        }

        public override void Initialize()
        {
 	        base.Initialize();
            m_GrapgMgr = (GraphicsDeviceManager)this.Game.Services.
                    GetService(typeof(GraphicsDeviceManager));
            m_Menu[(int)ScreenMenu.FullScreen].Value = toStr(m_GrapgMgr.IsFullScreen);
            m_Menu[(int)ScreenMenu.Resize].Value = toStr(this.Game.Window.AllowUserResizing);
            m_Menu[(int)ScreenMenu.Mouse].Value = toStr(this.Game.IsMouseVisible);
        }

        private void m_Menu_MenuItemPgDown(object sender, MenuEventArgs e)
        {
            switch (e.MenuItemIndex)
            {
                case (int)ScreenMenu.FullScreen:
                    m_GrapgMgr.ToggleFullScreen();
                    m_Menu[(int)ScreenMenu.FullScreen].Value = toStr(m_GrapgMgr.IsFullScreen);
                    break;
                case (int)ScreenMenu.Resize:
                    this.Game.Window.AllowUserResizing = !this.Game.Window.AllowUserResizing;
                    m_Menu[(int)ScreenMenu.Resize].Value = toStr(this.Game.Window.AllowUserResizing);
                    break;
                case (int)ScreenMenu.Mouse:
                    this.Game.IsMouseVisible = !this.Game.IsMouseVisible;
                    m_Menu[(int)ScreenMenu.Mouse].Value = toStr(this.Game.IsMouseVisible);
                    break;
                default:
                    break;
            }
        }

        private void m_Menu_MenuItemEntered(object sender, MenuEventArgs e)
        {
            switch (e.MenuItemIndex)
            {
                case (int)ScreenMenu.Done:
                    this.ExitScreen();
                    break;
                default:
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (this.InputManager.KeyPressed(Keys.Escape))
            {
                this.ExitScreen();
            }
            else if (this.InputManager.KeyPressed(Keys.M))
            {
                (Game.Services.GetService(typeof(ISoundManeger)) as ISoundManeger).ToggleMute();
            }

            base.Update(gameTime);
        }
    }
}
